package com.xqboss.common.utils;

import com.xqboss.common.exception.ServiceException;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.ValidatorFactory;
import java.util.Set;

public class ValidatorUtil {

    public static <T> void valid(T object){
        // 做下手动参数校验
        ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
        Set<ConstraintViolation<T>> validate = validatorFactory.getValidator().validate(object);
        validatorFactory.close();
        if(!validate.isEmpty()){
            for (ConstraintViolation<T> va : validate) {
                throw new ServiceException(va.getMessage());
            }
        }
    }

}
